import Vue from 'vue'
import Vuex from 'vuex'
import API from '@/network/api/pmsApi'
import { isMobile } from '@/utils/common'
import { createSign } from '@/utils/sign'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    oemUserInfo: JSON.parse(localStorage.getItem('loginUserInfo')) || {},
    // 用于修改setting页面的currentPage
    settingCurrentPage: 1,
    headerPath: JSON.parse(localStorage.getItem('updateUserInfo')) === undefined || JSON.parse(localStorage.getItem('updateUserInfo')) === null ? '' : JSON.parse(localStorage.getItem('updateUserInfo')).headerPath,
    dataRow: null, 
  },
  mutations: {
    setUserInfo(state, info) {
      state.oemUserInfo = info
      state.oemUserInfo.dlLevel = JSON.parse(localStorage.getItem('updateUserInfo')).dlLevel
      // console.log(info);
      console.log(state.oemUserInfo)
    },
    editSettingCurrentPage(state) {
      state.settingCurrentPage++
    },
    editHeaderPath(state, currentHeaderPath) {
      state.headerPath = currentHeaderPath
    },
    orderManage(state, row) {
      state.dataRow = row
    }
  },
  actions: {
    async loginInfo({ commit }) {
      const info = JSON.parse(localStorage.getItem('updateUserInfo'))
      // console.log(info)
      const userInfo = {
        organId: info.organId, // 机构id
        timestamp: new Date().getTime()
      }
      console.log(userInfo);
      const res = await API.getLoginDetail({ ...userInfo, sign: createSign(JSON.stringify(userInfo)) })
      if (res.data.code === 0) {
        // console.log(res.data.data);
        commit('setUserInfo', res.data.data)
      } else {
        if (isMobile()) {
          Toast.fail(res.data.msg)
        } else {
          Vue.prototype.$message({
            type: 'warning',
            message: res.data.msg
          })
        }
      }
    }
  },
  modules: {}
})
